home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / SimpleVoice / frmApp.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  16.4 KB  |  352 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Begin VB.Form frmApp 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Simple Voice"
  6.    ClientHeight    =   3465
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4755
  10.    Icon            =   "frmApp.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   3465
  15.    ScaleWidth      =   4755
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin MSComctlLib.ListView lvMembers 
  18.       Height          =   3075
  19.       Left            =   120
  20.       TabIndex        =   0
  21.       Top             =   300
  22.       Width           =   4575
  23.       _ExtentX        =   8070
  24.       _ExtentY        =   5424
  25.       View            =   3
  26.       LabelEdit       =   1
  27.       LabelWrap       =   -1  'True
  28.       HideSelection   =   -1  'True
  29.       _Version        =   393217
  30.       ForeColor       =   -2147483640
  31.       BackColor       =   -2147483643
  32.       BorderStyle     =   1
  33.       Appearance      =   1
  34.       NumItems        =   2
  35.       BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  36.          Text            =   "Name"
  37.          Object.Width           =   2540
  38.       EndProperty
  39.       BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  40.          SubItemIndex    =   1
  41.          Text            =   "Status"
  42.          Object.Width           =   2469
  43.       EndProperty
  44.    End
  45.    Begin VB.Label lblInfo 
  46.       BackStyle       =   0  'Transparent
  47.       Caption         =   "Members of this conversation:"
  48.       Height          =   255
  49.       Left            =   180
  50.       TabIndex        =   1
  51.       Top             =   60
  52.       Width           =   3855
  53.    End
  54. Attribute VB_Name = "frmApp"
  55. Attribute VB_GlobalNameSpace = False
  56. Attribute VB_Creatable = False
  57. Attribute VB_PredeclaredId = True
  58. Attribute VB_Exposed = False
  59. Option Explicit
  60. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  61. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  62. '  File:       frmApp.frm
  63. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  64. Implements DirectPlay8Event
  65. Implements DirectPlayVoiceEvent8
  66. Private Sub Form_Load()
  67.     'Init our vars
  68.     InitDPlay
  69.     'Now we can create a new Connection Form (which will also be our message pump)
  70.     Set DPlayEventsForm = New DPlayConnect
  71.     'First lets get the dplay connection started
  72.     If Not DPlayEventsForm.StartConnectWizard(dx, dpp, AppGuid, 10, Me) Then
  73.         Unload Me
  74.     End If
  75.     'Am I the host?
  76.     fAmHost = DPlayEventsForm.IsHost
  77.     'First let's set up the DirectPlayVoice stuff since that's the point of this demo
  78.     If fAmHost Then
  79.         'After we've created the session and let's start
  80.         'the DplayVoice server
  81.         Dim oSession As DVSESSIONDESC
  82.         
  83.         'Create our DPlayVoice Server
  84.         Set dvServer = dx.DirectPlayVoiceServerCreate
  85.             
  86.         'Set up the Session
  87.         oSession.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  88.         oSession.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  89.         oSession.lSessionType = DVSESSIONTYPE_PEER
  90.         oSession.guidCT = vbNullString
  91.         
  92.         'Init and start the session
  93.         dvServer.Initialize dpp, 0
  94.         dvServer.StartSession oSession, 0
  95.         Dim oSound As DVSOUNDDEVICECONFIG
  96.         Dim oClient As DVCLIENTCONFIG
  97.         'Now create a client as well (so we can both talk and listen)
  98.         Set dvClient = dx.DirectPlayVoiceClientCreate
  99.         'Now let's create a client event..
  100.         dvClient.StartClientNotification Me
  101.         dvClient.Initialize dpp, 0
  102.         'Set up our client and sound structs
  103.         oClient.lFlags = DVCLIENTCONFIG_AUTOVOICEACTIVATED Or DVCLIENTCONFIG_AUTORECORDVOLUME
  104.         oClient.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  105.         oClient.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  106.         oClient.lNotifyPeriod = 0
  107.         oClient.lThreshold = DVTHRESHOLD_UNUSED
  108.         oClient.lPlaybackVolume = DVPLAYBACKVOLUME_DEFAULT
  109.         oSound.hwndAppWindow = Me.hwnd
  110.         
  111.         On Error Resume Next
  112.         'Connect the client
  113.         dvClient.Connect oSound, oClient, 0
  114.         If Err.Number = DVERR_RUN_SETUP Then    'The audio tests have not been run on this
  115.                                                 'machine.  Run them now.
  116.             'we need to run setup first
  117.             Dim dvSetup As DirectPlayVoiceTest8
  118.             
  119.             Set dvSetup = dx.DirectPlayVoiceTestCreate
  120.             dvSetup.CheckAudioSetup vbNullString, vbNullString, Me.hwnd, 0 'Check the default devices since that's what we'll be using
  121.             If Err.Number = DVERR_COMMANDALREADYPENDING Then
  122.                 MsgBox "Could not start DirectPlayVoice.  The Voice Networking wizard is already open.  This sample must exit.", vbOKOnly Or vbInformation, "No Voice"
  123.                 Cleanup
  124.                 Unload Me
  125.                 End
  126.             End If
  127.             If Err.Number = DVERR_USERCANCEL Then
  128.                 MsgBox "Could not start DirectPlayVoice.  The Voice Networking wizard has been cancelled.  This sample must exit.", vbOKOnly Or vbInformation, "No Voice"
  129.                 Cleanup
  130.                 Unload Me
  131.                 End
  132.             End If
  133.             Set dvSetup = Nothing
  134.             dvClient.Connect oSound, oClient, 0
  135.         ElseIf Err.Number <> 0 And Err.Number <> DVERR_PENDING Then
  136.             MsgBox "Could not start DirectPlayVoice.  This sample must exit." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbCritical, "Exiting"
  137.             Cleanup
  138.             Unload Me
  139.             End
  140.         End If
  141.     End If
  142. End Sub
  143. Private Sub UpdateList(ByVal lPlayerID As Long, fTalking As Boolean)
  144.     Dim lCount As Long
  145.     For lCount = lvMembers.ListItems.Count To 1 Step -1
  146.         If lvMembers.ListItems.Item(lCount).Key = "K" & CStr(lPlayerID) Then
  147.             'Change this guys status
  148.             If fTalking Then
  149.                 lvMembers.ListItems.Item(lCount).SubItems(1) = "Talking"
  150.             Else
  151.                 lvMembers.ListItems.Item(lCount).SubItems(1) = "Silent"
  152.             End If
  153.         End If
  154.     Next
  155. End Sub
  156. Private Sub Form_Unload(Cancel As Integer)
  157.     Me.Hide
  158.     DPlayEventsForm.DoSleep 50
  159.     Cleanup
  160. End Sub
  161. Public Sub UpdatePlayerList()
  162.     'Get everyone who is currently in the session and add them if we don't have them currently.
  163.     Dim lCount As Long
  164.     Dim Player As DPN_PLAYER_INFO
  165.     ' Enumerate players
  166.     For lCount = 1 To dpp.GetCountPlayersAndGroups(DPNENUM_PLAYERS)
  167.         If Not (AmIInList(dpp.GetPlayerOrGroup(lCount))) Then 'Add this player
  168.             Dim lItem As ListItem, sName As String
  169.             Player = dpp.GetPeerInfo(dpp.GetPlayerOrGroup(lCount))
  170.             sName = Player.Name
  171.             If sName = vbNullString Then sName = "Unknown"
  172.             If (Player.lPlayerFlags And DPNPLAYER_LOCAL = DPNPLAYER_LOCAL) Then glMyID = dpp.GetPlayerOrGroup(lCount)
  173.             Set lItem = lvMembers.ListItems.Add(, "K" & CStr(dpp.GetPlayerOrGroup(lCount)), sName)
  174.             lItem.SubItems(1) = "Silent"
  175.         End If
  176.     Next lCount
  177. End Sub
  178. Private Function AmIInList(ByVal lPlayerID As Long) As Boolean
  179.     Dim lCount As Long, fInThis As Boolean
  180.     For lCount = lvMembers.ListItems.Count To 1 Step -1
  181.         If lvMembers.ListItems.Item(lCount).Key = "K" & CStr(lPlayerID) Then
  182.             fInThis = True
  183.         End If
  184.     Next
  185.     AmIInList = fInThis
  186. End Function
  187. Private Sub RemovePlayer(ByVal lPlayerID As Long)
  188.     Dim lCount As Long
  189.     For lCount = lvMembers.ListItems.Count To 1 Step -1
  190.         If lvMembers.ListItems.Item(lCount).Key = "K" & CStr(lPlayerID) Then
  191.             lvMembers.ListItems.Remove lCount
  192.         End If
  193.     Next
  194. End Sub
  195. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  196.     'VB requires that we must implement *every* member of this interface
  197. End Sub
  198. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  199.     'VB requires that we must implement *every* member of this interface
  200. End Sub
  201. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  202.     'VB requires that we must implement *every* member of this interface
  203. End Sub
  204. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  205.     'Now we're connected, start our voice session
  206.     Dim oSound As DVSOUNDDEVICECONFIG
  207.     Dim oClient As DVCLIENTCONFIG
  208.     If dpnotify.hResultCode <> 0 Then
  209.         'For some reason we could not connect.  All available slots must be closed.
  210.         MsgBox "Connect Failed.  Error: 0x" & CStr(Hex$(dpnotify.hResultCode)) & "  - This sample will now close.", vbOKOnly Or vbCritical, "Closing"
  211.         DPlayEventsForm.CloseForm Me
  212.     Else
  213.         'Now create a client as well (so we can both talk and listen)
  214.         Set dvClient = dx.DirectPlayVoiceClientCreate
  215.         
  216.         'Now let's create a client event..
  217.         dvClient.StartClientNotification Me
  218.         
  219.         dvClient.Initialize dpp, 0
  220.         'Set up our client and sound structs
  221.         oClient.lFlags = DVCLIENTCONFIG_AUTOVOICEACTIVATED Or DVCLIENTCONFIG_AUTORECORDVOLUME
  222.         oClient.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  223.         oClient.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  224.         oClient.lNotifyPeriod = 0
  225.         oClient.lThreshold = DVTHRESHOLD_UNUSED
  226.         oClient.lPlaybackVolume = DVPLAYBACKVOLUME_DEFAULT
  227.         oSound.hwndAppWindow = Me.hwnd
  228.         
  229.         On Error Resume Next
  230.         'Connect the client
  231.         dvClient.Connect oSound, oClient, 0
  232.         If Err.Number = DVERR_RUN_SETUP Then    'The audio tests have not been run on this
  233.                                                 'machine.  Run them now.
  234.             'we need to run setup first
  235.             Dim dvSetup As DirectPlayVoiceTest8
  236.             
  237.             Set dvSetup = dx.DirectPlayVoiceTestCreate
  238.             dvSetup.CheckAudioSetup vbNullString, vbNullString, Me.hwnd, 0 'Check the default devices since that's what we'll be using
  239.             If Err.Number = DVERR_COMMANDALREADYPENDING Then
  240.                 MsgBox "Could not start DirectPlayVoice.  The Voice Networking wizard is already open.  This sample must exit.", vbOKOnly Or vbInformation, "No Voice"
  241.                 DPlayEventsForm.CloseForm Me
  242.             End If
  243.             If Err.Number = DVERR_USERCANCEL Then
  244.                 MsgBox "Could not start DirectPlayVoice.  The Voice Networking wizard has been cancelled.  This sample must exit.", vbOKOnly Or vbInformation, "No Voice"
  245.                 DPlayEventsForm.CloseForm Me
  246.             End If
  247.             Set dvSetup = Nothing
  248.             dvClient.Connect oSound, oClient, 0
  249.         ElseIf Err.Number <> 0 And Err.Number <> DVERR_PENDING Then
  250.             MsgBox "Could not start DirectPlayVoice.  This sample must exit." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbCritical, "Exiting"
  251.             DPlayEventsForm.CloseForm Me
  252.             Exit Sub
  253.         End If
  254.     End If
  255. End Sub
  256. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  257.     'VB requires that we must implement *every* member of this interface
  258. End Sub
  259. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  260.     'VB requires that we must implement *every* member of this interface
  261. End Sub
  262. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  263.     'VB requires that we must implement *every* member of this interface
  264. End Sub
  265. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  266.     'VB requires that we must implement *every* member of this interface
  267. End Sub
  268. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  269.     'VB requires that we must implement *every* member of this interface
  270. End Sub
  271. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  272.     'VB requires that we must implement *every* member of this interface
  273. End Sub
  274. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  275.     'VB requires that we must implement *every* member of this interface
  276. End Sub
  277. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  278.     'VB requires that we must implement *every* member of this interface
  279. End Sub
  280. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  281.     'VB requires that we must implement *every* member of this interface
  282. End Sub
  283. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  284.     'VB requires that we must implement *every* member of this interface
  285. End Sub
  286. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  287.     'VB requires that we must implement *every* member of this interface
  288. End Sub
  289. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  290.     'VB requires that we must implement *every* member of this interface
  291. End Sub
  292. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  293.     'VB requires that we must implement *every* member of this interface
  294. End Sub
  295. Private Sub DirectPlayVoiceEvent8_ConnectResult(ByVal ResultCode As Long)
  296.     Dim lTargets(0) As Long
  297.     If ResultCode = 0 Then
  298.         lTargets(0) = DVID_ALLPLAYERS
  299.         dvClient.SetTransmitTargets lTargets, 0
  300.         
  301.         'Update the list
  302.         UpdatePlayerList
  303.     Else
  304.         MsgBox "Could not start DirectPlayVoice.  This sample must exit." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbCritical, "Exiting"
  305.         DPlayEventsForm.CloseForm Me
  306.     End If
  307. End Sub
  308. Private Sub DirectPlayVoiceEvent8_CreateVoicePlayer(ByVal playerID As Long, ByVal flags As Long)
  309.     'Someone joined, update the player list
  310.     UpdatePlayerList
  311. End Sub
  312. Private Sub DirectPlayVoiceEvent8_DeleteVoicePlayer(ByVal playerID As Long)
  313.     'Someone quit, remove them from the session
  314.     RemovePlayer playerID
  315. End Sub
  316. Private Sub DirectPlayVoiceEvent8_DisconnectResult(ByVal ResultCode As Long)
  317.     'VB requires that we must implement *every* member of this interface
  318. End Sub
  319. Private Sub DirectPlayVoiceEvent8_HostMigrated(ByVal NewHostID As Long, ByVal NewServer As DxVBLibA.DirectPlayVoiceServer8)
  320.     'VB requires that we must implement *every* member of this interface
  321. End Sub
  322. Private Sub DirectPlayVoiceEvent8_InputLevel(ByVal PeakLevel As Long, ByVal RecordVolume As Long)
  323.     'VB requires that we must implement *every* member of this interface
  324. End Sub
  325. Private Sub DirectPlayVoiceEvent8_OutputLevel(ByVal PeakLevel As Long, ByVal OutputVolume As Long)
  326.     'VB requires that we must implement *every* member of this interface
  327. End Sub
  328. Private Sub DirectPlayVoiceEvent8_PlayerOutputLevel(ByVal playerID As Long, ByVal PeakLevel As Long)
  329.     'VB requires that we must implement *every* member of this interface
  330. End Sub
  331. Private Sub DirectPlayVoiceEvent8_PlayerVoiceStart(ByVal playerID As Long)
  332.     'Someone is talking, update the list
  333.     UpdateList playerID, True
  334. End Sub
  335. Private Sub DirectPlayVoiceEvent8_PlayerVoiceStop(ByVal playerID As Long)
  336.     'Someone stopped talking, update the list
  337.     UpdateList playerID, False
  338. End Sub
  339. Private Sub DirectPlayVoiceEvent8_RecordStart(ByVal PeakVolume As Long)
  340.     'I am talking, update the list
  341.     UpdateList glMyID, True
  342. End Sub
  343. Private Sub DirectPlayVoiceEvent8_RecordStop(ByVal PeakVolume As Long)
  344.     'I have quit talking, update the list
  345.     UpdateList glMyID, False
  346. End Sub
  347. Private Sub DirectPlayVoiceEvent8_SessionLost(ByVal ResultCode As Long)
  348.     'The voice session has exited, let's quit
  349.     MsgBox "The DirectPlayVoice session was lost.  This sample is exiting.", vbOKOnly Or vbInformation, "Session lost."
  350.     DPlayEventsForm.CloseForm Me
  351. End Sub
  352.